package com.baihan.angus.app.game.busshow.pop_dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.baihan.angus.app.game.basic.utils.AppUtil;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.busshow.adps.ClueDialogTitleAdp;
import com.baihan.angus.app.game.busshow.adps.SceneRenderAdp;
import com.baihan.angus.app.game.common.bean.response.SceneRenderResponse;

import java.util.List;
import java.util.stream.Collectors;

public class SceneRenderDialog extends Dialog {

    private Context mContext;
    private Callback mCallback;
    private WindowManager.LayoutParams mLayoutParams;
    private TextView tvTitle;
    private ImageView ivClose;
    private RecyclerView rvTitle, rvDatas;
    private LinearLayout llNoData;

    private SceneRenderAdp mSceneRenderAdp;

    private ClueDialogTitleAdp mClueDialogTitleAdp;
    private SceneRenderResponse.DataX sceneData;
    private List<String> titleList;
    private String title;
    private int titlePosition = 0;

    public SceneRenderDialog(@NonNull Context context, Callback mCallback) {
        super(context, R.style.FullDialog);
        this.mContext = context;
        this.mCallback = mCallback;
        initContentView();
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        Window dialogWindow = this.getWindow();
        mLayoutParams = dialogWindow.getAttributes();
        mLayoutParams.width = AppUtil.getScreenWidth(context);
        mLayoutParams.height = (int) (AppUtil.getScreenHeight(context) * 0.8);
        dialogWindow.setAttributes(mLayoutParams);
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        dialogWindow.getDecorView().setBackgroundResource(android.R.color.transparent);
        dialogWindow.setGravity(Gravity.BOTTOM);
    }

    private void initContentView() {
        View dialogView = LayoutInflater.from(this.getContext()).inflate(R.layout.dialog_scene_render, (ViewGroup) null);
        setContentView(dialogView);
        tvTitle = dialogView.findViewById(R.id.tvTitle);
        rvTitle = dialogView.findViewById(R.id.rvTitle);
        ivClose = dialogView.findViewById(R.id.ivClose);
        rvDatas = dialogView.findViewById(R.id.rvDatas);
        llNoData = dialogView.findViewById(R.id.llNoData);

        mClueDialogTitleAdp = new ClueDialogTitleAdp();
        rvTitle.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
        mClueDialogTitleAdp.bindToRecycleView(rvTitle);

        mClueDialogTitleAdp.setOnItemClickListener(new ClueDialogTitleAdp.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                titlePosition = position;
                mClueDialogTitleAdp.setThisPosition(position);
                mClueDialogTitleAdp.notifyDataSetChanged();
                title = sceneData.getScene_data().get(position).getTitle();
                tvTitle.setText(title);
                mSceneRenderAdp.clear();
                if (null != sceneData.getScene_data().get(position).getData() && sceneData.getScene_data().get(position).getData().size() > 0) {
                    mSceneRenderAdp.addAll(sceneData.getScene_data().get(position).getData());
                    rvDatas.setVisibility(View.VISIBLE);
                    llNoData.setVisibility(View.GONE);
                } else {
                    rvDatas.setVisibility(View.GONE);
                    llNoData.setVisibility(View.VISIBLE);
                }
            }
        });

        mSceneRenderAdp = new SceneRenderAdp();
        rvDatas.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
        mSceneRenderAdp.bindToRecycleView(rvDatas);

        mSceneRenderAdp.setOnItemClickListener(new SceneRenderAdp.OnItemClickListener() {
            @Override
            public void onPlay(int position) {
                if (null != mCallback) {
                    mCallback.play(mSceneRenderAdp.getItem(position));
                }
            }
        });

        ivClose.setOnClickListener(v -> {
            cancel();
        });

    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    public void initData(SceneRenderResponse.DataX data) {
        sceneData = data;
        mClueDialogTitleAdp.clear();
        mSceneRenderAdp.clear();
        //该方法6.0不兼容
        titleList = data.getScene_data().stream().map(SceneRenderResponse.DataX.SceneData::getTitle).collect(Collectors.toList());
        mClueDialogTitleAdp.addAll(titleList);
        title = data.getScene_data().get(titlePosition).getTitle();
        tvTitle.setText(title);
        if (null != data.getScene_data().get(titlePosition).getData() && data.getScene_data().get(titlePosition).getData().size() > 0) {
            mSceneRenderAdp.addAll(data.getScene_data().get(titlePosition).getData());
            rvDatas.setVisibility(View.VISIBLE);
            llNoData.setVisibility(View.GONE);
        } else {
            rvDatas.setVisibility(View.GONE);
            llNoData.setVisibility(View.VISIBLE);
        }
        mClueDialogTitleAdp.setThisPosition(titlePosition);
    }

    public interface Callback {
        void play(SceneRenderResponse.DataX.SceneData.Data data);
    }
}
